<template>
  <div>
    <!-- echart饼图 -->
    <div :id="id" style="width:420px;height:410px"></div>
  </div>
</template>
 
<script>
export default {
  props: {
    id: {
      type: String,
      default: ""
    },
    // 标题文本
    titleText: {
      type: String,
      default: ""
    }, 
     // 提示框键名
    tooltipFormatter: {
    type: String,
    default: ''
    },
    // 扇形区域名称
    opinion: {
    type: Array,
    default(){
    return []
    }
    },
    // 提示框标题
    seriesName: {
    type: String,
    default: ''
    },
    // 扇形区域数据
    opinionData: {
      type: Array,
      default() {
        return [];
      }
    },
    // 样式
    width: {
      type: String,
      default: "420px"
    },
    height: {
      type: String,
      default: "360px"
    }
  },
  data() {
    return {};
  },
  computed: {
    style() {
      return {
        height: this.height,
        width: this.width
      };
    }
  },
  mounted() {
    this.$nextTick(function() {
      this.drawPie(this.id);
    });
  },
  watch: {
    //观察opinionData的变化
    opinionData: {
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.chart.setOption(newVal);
          } else {
            this.chart.setOption(oldVal);
          }
        } else {
          this.drawPie(id);
        }
      },
      deep: true //对象内部属性的监听，关键。
    }
  },
  methods: {
    drawPie(id) {
      this.charts = this.$echarts.init(document.getElementById(this.id));
      this.charts.setOption({
        title: {
          text: this.titleText, // 标题文本
          x: "center", //位置
          y: "2%",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} :{d}%"
        },
        color: [
          "rgb(149, 163, 189)",
          "rgb(159, 189, 178)",
          "rgb(159, 193, 199)",
          "rgb(173, 149, 199)",
          "rgb(158, 169, 149)"
        ],
        series: [
          {
            name: this.seriesName, // 提示框标题
            type: "pie",
            radius: "65%",
            center: ["52%", "55%"],
            selectedMode: "single",
            data: this.opinionData, // 扇形区域数据
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            }
          }
        ]
      });
    }
  }
};
</script>
 
<style>
/* #myChart {
    width: 840px;
    height: 360px;
}  */
</style>
  
  

